using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServerWeb.ControlPanel.Moderation
{
	/// <summary>
	/// Summary description for RenameTag.
	/// </summary>
	public class RenameTag  : System.Web.UI.Page
	{
		CSContext context = CSContext.Current;

		#region Controls

		protected TextBox NewName;
		protected Literal OldName;
		protected ResourceLinkButton RenameButton;

		#endregion

		#region Properties

		private string Tag
		{
			get
			{
				if (context.Tags.Length > 0)
					return context.Tags[0];
				else
					return string.Empty;
			}
		}

		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			CommunityServer.Controls.Title.Set(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Moderation_RenameTag_Title"), this.Context);

			if (!Page.IsPostBack)
			{
				if (Tag != string.Empty)
					Bind();
			}
		}

		private void Bind()
		{
			OldName.Text = this.Tag;
		}

		private void RenameButton_Click(object sender, EventArgs e)
		{
			if (this.IsValid)
			{
				ArrayList postCategoriesFrom = PostCategories.GetCategories(this.Tag, ApplicationType.Forum);
				Hashtable postCategoriesTo = new Hashtable();
				foreach (PostCategory pc in PostCategories.GetCategories(NewName.Text.Trim(), ApplicationType.Forum))
				{
					postCategoriesTo.Add(pc.SectionID, pc);
				}

				foreach (PostCategory pcFrom in postCategoriesFrom)
				{
					if (!postCategoriesTo.ContainsKey(pcFrom.SectionID))
					{
						// simple rename :-)
						pcFrom.Name = NewName.Text.Trim();
						PostCategories.UpdateCategory(pcFrom);
					}
					else
					{
						// "to" tag already exists, update all posts to ensure they have "to" tag and delete "from" tag
						ArrayList postsFrom = Tags.GetPostsMatchingTags(pcFrom.SectionID, new string[] { pcFrom.Name }, 0, 9999999, true, false).Posts;
						Hashtable postsTo = new Hashtable();
						foreach (IndexPost post in Tags.GetPostsMatchingTags(pcFrom.SectionID, new string[] { NewName.Text.Trim() }, 0, 999999, true, false).Posts)
						{
							postsTo.Add(post.PostID, post);
						}

						foreach(IndexPost post in postsFrom)
						{
							if (!postsTo.ContainsKey(post.PostID))
							{
								// add "to" tag to post
								ForumPost realPost = Posts.GetPost(post.PostID, context.User.UserID, false, false, true);
								
								ArrayList categories = new ArrayList(realPost.Categories);
								categories.Add(NewName.Text.Trim());
								realPost.Categories = (string[]) categories.ToArray(typeof(string));
								
								Posts.UpdatePost(realPost, context.User.UserID);
							}
						}

						PostCategories.DeleteCategory(pcFrom.CategoryID, pcFrom.SectionID);
					}
				}

				Modal.ClosePage(Page,"true") ;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
			this.RenameButton.Click += new EventHandler(RenameButton_Click);
		}
		#endregion
	}
}
